﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FlatFileParser.Core.Attributes;
using FlatFileParser.Core.Enums;
using FlatFileParser.Core.Processor;

namespace Tester
{
    [ProcessorType(LineProcessingType.Full)]
    public class SecondClass
    {
        [FullProcessorField(1, 26)]
        public string Value { get; set; }

        [FullProcessorField(2, 6)]
        public float Amount { get; set; }

        public static SecondClass ParseValue(string value)
        {
            var processor = new FileProcessor<SecondClass>();
            IEnumerable<SecondClass> values = processor.ProcessString(value);

            if (values.Count() > 0)
            {
                return values.First();
            }

            return new SecondClass();
        }

        public override string ToString()
        {
            return Value.PadRight(26, ' ') + (Amount + 100).ToString().PadLeft(6, '0');
        }
    }

    [ProcessorType(LineProcessingType.Full)]
    public class Tester
    {
        [FullProcessorField(1, 5)]
        public int ID { get; set; }

        [FullProcessorField(2, 32, "ParseValue")]
        public SecondClass Value { get; set; }

        //[FullProcessorField(2, 26)]
        //public string Value { get; set; }

        //[FullProcessorField(28, 6)]
        //public float Amount { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            FileProcessor<Tester> processor = new FileProcessor<Tester>();

            processor.Setup.Add(ProcessingSetup.SkipLineOnFailure);

            //foreach (var record in processor.ProcessFile("Test.txt"))
            //{
            //    //Console.WriteLine("    ID:\t" + record.ID);
            //    //Console.WriteLine("  Text:\t" + record.Value.Value);
            //    //Console.WriteLine("Amount:\t" + record.Value.Amount);
            //    //Console.WriteLine();
            //}

            //Console.WriteLine("These lines failed:");
            //foreach (var line in processor.ErrorLines)
            //{
            //    Console.WriteLine(line);
            //}

            var records = processor.ProcessFile("Test.txt");
            //processor.WriteToFile(records, "c:\\test.txt");

            Console.ReadLine();
        }
    }
}
